import React from 'react';
class CountDown extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        time: parseInt(localStorage.getItem("countdown_time")) || props.time,
      };
    }
  
    componentDidMount() {
      this.interval = setInterval(() => {
        if (this.state.time <= 0) {
          clearInterval(this.interval);
          this.props.onFinish && this.props.onFinish();
        } else {
          this.setState({ time: this.state.time - 1 });
          localStorage.setItem("countdown_time", this.state.time - 1);
        }
      }, 1000);
    }
  
    componentWillUnmount() {
      clearInterval(this.interval);
    }
  
    render() {
      const { time } = this.state;
      const minutes = Math.floor(time / 60);
      const seconds = time % 60;
  
      return (
        <div>
          {minutes.toString().padStart(2, "0")}:{seconds.toString().padStart(2, "0")}
        </div>
      );
    }
  }
  
export default CountDown
  